﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.IO;

namespace kiosk
{
    public partial class Margin_Window : Form
    {
        private bool nonNumberEntered = false;
        int menuOpt = -1;
        
        
        public Margin_Window()
        {
            InitializeComponent();
        }

        
        public Margin_Window(int menuOpt)
        {

            InitializeComponent();

            this.nLabelTxt.KeyDown += new KeyEventHandler(NBtnText_KeyDown);
            this.TopMarTxt.KeyDown += new KeyEventHandler(NBtnText_KeyDown);
            this.bMarTxt.KeyDown += new KeyEventHandler(NBtnText_KeyDown);
            this.leftMarTxt.KeyDown += new KeyEventHandler(NBtnText_KeyDown);
            this.rMarTxt.KeyDown += new KeyEventHandler(NBtnText_KeyDown);
            this.heightTxt.KeyDown += new KeyEventHandler(NBtnText_KeyDown);
            this.widthTxt.KeyDown += new KeyEventHandler(NBtnText_KeyDown);


            this.nLabelTxt.KeyPress += new KeyPressEventHandler(NBtnText_KeyPress);
            this.TopMarTxt.KeyPress += new KeyPressEventHandler(NBtnText_KeyPress);
            this.bMarTxt.KeyPress += new KeyPressEventHandler(NBtnText_KeyPress);
            this.leftMarTxt.KeyPress += new KeyPressEventHandler(NBtnText_KeyPress);
            this.rMarTxt.KeyPress += new KeyPressEventHandler(NBtnText_KeyPress);
            this.heightTxt.KeyPress += new KeyPressEventHandler(NBtnText_KeyPress);
            this.widthTxt.KeyPress += new KeyPressEventHandler(NBtnText_KeyPress);



            this.menuOpt = menuOpt;

        }

        private void Margin_Window_Load(object sender, EventArgs e)
        {
            String settingFpath = System.IO.Path.Combine(Application.StartupPath, GeneralOpt.AppSettingsFileName);
            if (File.Exists(settingFpath))
            {

                // XElement docu = XElement.Load(settingFpath);
                XDocument docu = XDocument.Load(settingFpath);
                if (!docu.Root.IsEmpty)
                {


                    XElement margin = docu.Root.Element("Margins");
                    if (margin != null )
                    {

                        if (this.menuOpt == 1)
                        {
                            this.nLabelTxt.Enabled = false;
                            XElement temp = margin.Element("MainWindow");
                            if (temp != null && temp.IsEmpty)
                            {
                                
                                this.bMarTxt.Text = temp.Attribute("Buttom").Value;
                                this.leftMarTxt.Text = temp.Attribute("Left").Value;
                                this.TopMarTxt.Text = temp.Attribute("Top").Value;
                                this.rMarTxt.Text = temp.Attribute("Right").Value;
                                this.widthTxt.Text = temp.Attribute("Width").Value;
                                this.heightTxt.Text = temp.Attribute("Height").Value;
                                


                            }
                        }

                        else if (this.menuOpt == 2)
                        {
                            // For the button window

                            XElement temp = margin.Element("ButtonWindow");
                            if (temp != null && !temp.IsEmpty)
                            {
                                this.bMarTxt.Text = temp.Attribute("Buttom").Value;
                                this.leftMarTxt.Text = temp.Attribute("Left").Value;
                                this.TopMarTxt.Text = temp.Attribute("Top").Value;
                                this.rMarTxt.Text = temp.Attribute("Right").Value;
                                this.widthTxt.Text = temp.Attribute("Width").Value;
                                this.heightTxt.Text = temp.Attribute("Height").Value;
                                this.nLabelTxt.Text = temp.Value;
                                //this.nLabelTxt.Enabled = true;




                            }



                        }


                        else if (this.menuOpt == 3)
                        {
                            // For the groupBackWindow window
                            this.nLabelTxt.Enabled = false;
                            XElement temp = margin.Element("GroupBackButton");
                            if (temp != null && !temp.IsEmpty)
                            {
                                this.bMarTxt.Text = temp.Attribute("Buttom").Value;
                                this.leftMarTxt.Text = temp.Attribute("Left").Value;
                                this.TopMarTxt.Text = temp.Attribute("Top").Value;
                                this.rMarTxt.Text = temp.Attribute("Right").Value;
                                this.widthTxt.Text = temp.Attribute("Width").Value;
                                this.heightTxt.Text = temp.Attribute("Height").Value;

                               




                            }



                        }




                    }







                }




            }







        }



        // the eventhandlers of the window
        #region eventHandlers

        private void NBtnText_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            // Initialize the flag to false.
            nonNumberEntered = false;

            // Determine whether the keystroke is a number from the top of the keyboard.
            if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
            {
                // Determine whether the keystroke is a number from the keypad.
                if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                {
                    // Determine whether the keystroke is a backspace.
                    if (e.KeyCode != Keys.Back)
                    {
                        // A non-numerical keystroke was pressed.
                        // Set the flag to true and evaluate in KeyPress event.
                        nonNumberEntered = true;
                    }
                }
            }
            //If shift key was pressed, it's not a number.
            if (Control.ModifierKeys == Keys.Shift)
            {
                nonNumberEntered = true;
            }


        }

        private void NBtnText_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            // Check for the flag being set in the KeyDown event.
            if (nonNumberEntered == true)
            {
                // Stop the character from being entered into the control since it is non-numerical.
                e.Handled = true;
            }

        }




        #endregion

        private void saveBtn_Click(object sender, EventArgs e)
        {

            // input can notbe empty

          
            
            if(this.leftMarTxt.Text.Trim() =="")
            {
                MessageBox.Show("Left mergin cannot be empty");
                return;
            
            }
            
            if(this.rMarTxt.Text.Trim()=="")
            {
                MessageBox.Show("Right margin can not be empty");
                return;
            
            
            }
            
            if(this.bMarTxt.Text.Trim()=="")
            {

                MessageBox.Show("Buttom margin can not be empty");
                return;
            
            
            }

            if(this.TopMarTxt.Text.Trim() =="")
            {
                MessageBox.Show("Top mergin can be empty");
                return;
            
            }
            
            if(widthTxt.Text.Trim() == "")
            {
                MessageBox.Show("Width can not be empty");
                return;
            
            }
            if(this.heightTxt.Text.Trim()=="")
            {
                MessageBox.Show("Height cannot be empty");
                return;
            
            }
            
            
            
            
            // The saving Logic of the custom margin Widdow








            if (this.menuOpt == 1)
            { 
            
               // save in the menu option of the MainWindow

                 String settingFpath = System.IO.Path.Combine(Application.StartupPath, GeneralOpt.AppSettingsFileName);
                 if (File.Exists(settingFpath))
                 {

                     // XElement docu = XElement.Load(settingFpath);
                     XDocument docu = XDocument.Load(settingFpath);
                     if (docu.Root.Element("Margins") == null)
                     {

                         // Adda new element in the margin 

                         docu.Root.Add(new XElement("Margins",
                             new XElement("MainWindow",
                                 new XAttribute("Top", this.TopMarTxt.Text),
                                 new XAttribute("Buttom", this.bMarTxt.Text),
                                 new XAttribute("Left", this.leftMarTxt.Text), 
                                 new XAttribute("Right", this.rMarTxt.Text), 
                                 new XAttribute("Width", this.widthTxt.Text),
                                 new XAttribute("Height", this.heightTxt.Text))));


                     }
                     else
                     {

                         if (docu.Root.Element("Margins").Element("MainWindow") == null)
                         {

                             docu.Root.Element("Margins").Add(new XElement("MainWindow", new XAttribute("Top", this.TopMarTxt.Text.Trim()), new XAttribute("Buttom", this.bMarTxt.Text.Trim()), new XAttribute("Left", this.leftMarTxt.Text.Trim()), new XAttribute("Right", this.rMarTxt.Text.Trim()), new XAttribute("Width", this.widthTxt.Text.Trim()))); 
                         
                         }
                         else 
                         {
                             docu.Root.Element("Margins").Element("MainWindow").SetAttributeValue("Top", this.TopMarTxt.Text.Trim());
                             docu.Root.Element("Margins").Element("MainWindow").SetAttributeValue("Buttom", this.bMarTxt.Text.Trim());
                             docu.Root.Element("Margins").Element("MainWindow").SetAttributeValue("Left", this.leftMarTxt.Text.Trim());
                             docu.Root.Element("Margins").Element("MainWindow").SetAttributeValue("Right", this.rMarTxt.Text.Trim());
                             docu.Root.Element("Margins").Element("MainWindow").SetAttributeValue("Width", this.widthTxt.Text.Trim());
                             docu.Root.Element("Margins").Element("MainWindow").SetAttributeValue("Height", this.heightTxt.Text.Trim()); 
                         
                         }

                     
                     
                     
                     
                     
                     }


                     docu.Save(settingFpath);
                 
                 
                 }



                 MessageBox.Show("Settings saves for mainwindow !!");
            
            
            }

            else if(menuOpt == 2)
            {
                // for the button window

                
                if (this.nLabelTxt.Text.Trim() == "")
                {
                    MessageBox.Show("No of button can not be empty");
                    return;

                }
                
                
                String settingFpath = System.IO.Path.Combine(Application.StartupPath, GeneralOpt.AppSettingsFileName);
                if (File.Exists(settingFpath))
                {

                    // XElement docu = XElement.Load(settingFpath);
                    XDocument docu = XDocument.Load(settingFpath);
                    if (docu.Root.Element("Margins") == null)
                    {

                        // Adda new element in the margin 

                        docu.Root.Add(new XElement("Margins",
                            new XElement("ButtonWindow", this.nLabelTxt.Text,
                                new XAttribute("Top", this.TopMarTxt.Text),
                                new XAttribute("Buttom", this.bMarTxt.Text),
                                new XAttribute("Left", this.leftMarTxt.Text),
                                new XAttribute("Right", this.rMarTxt.Text),
                                new XAttribute("Width", this.widthTxt.Text),
                                new XAttribute("Height", this.heightTxt.Text))));


                    }
                    else
                    {

                        if (docu.Root.Element("Margins").Element("ButtonWindow") == null)
                        {

                            docu.Root.Element("Margins").Add(new XElement("ButtonWindow",this.nLabelTxt.Text.Trim(), new XAttribute("Top", this.TopMarTxt.Text.Trim()), new XAttribute("Buttom", this.bMarTxt.Text.Trim()), new XAttribute("Left", this.leftMarTxt.Text.Trim()), new XAttribute("Right", this.rMarTxt.Text.Trim()), new XAttribute("Width", this.widthTxt.Text.Trim()), new XAttribute("Height",this.heightTxt.Text.Trim())));

                        }
                        else
                        {
                            docu.Root.Element("Margins").SetElementValue("ButtonWindow",this.nLabelTxt.Text.Trim());
                            docu.Root.Element("Margins").Element("ButtonWindow").SetAttributeValue("Top", this.TopMarTxt.Text.Trim());
                            docu.Root.Element("Margins").Element("ButtonWindow").SetAttributeValue("Buttom", this.bMarTxt.Text.Trim());
                            docu.Root.Element("Margins").Element("ButtonWindow").SetAttributeValue("Left", this.leftMarTxt.Text.Trim());
                            docu.Root.Element("Margins").Element("ButtonWindow").SetAttributeValue("Right", this.rMarTxt.Text.Trim());
                            docu.Root.Element("Margins").Element("ButtonWindow").SetAttributeValue("Width", this.widthTxt.Text.Trim());
                            docu.Root.Element("Margins").Element("ButtonWindow").SetAttributeValue("Height", this.heightTxt.Text.Trim());
                    


                        }






                    }


                    docu.Save(settingFpath);


                }



                MessageBox.Show("Settings saved for the Label Window");
            
            }

            else if(this.menuOpt == 3)
            {


                //Settings for the GroupBackButton
                String settingFpath = System.IO.Path.Combine(Application.StartupPath, GeneralOpt.AppSettingsFileName);
                if (File.Exists(settingFpath))
                {

                    // XElement docu = XElement.Load(settingFpath);
                    XDocument docu = XDocument.Load(settingFpath);
                    if (docu.Root.Element("Margins") == null)
                    {

                        // Adda new element in the margin 

                        docu.Root.Add(new XElement("Margins",
                            new XElement("GroupBackButton",
                                new XAttribute("Top", this.TopMarTxt.Text),
                                new XAttribute("Buttom", this.bMarTxt.Text),
                                new XAttribute("Left", this.leftMarTxt.Text),
                                new XAttribute("Right", this.rMarTxt.Text),
                                new XAttribute("Width", this.widthTxt.Text),
                                new XAttribute("Height", this.heightTxt.Text))));


                    }
                    else
                    {

                        if (docu.Root.Element("Margins").Element("GroupBackButton") == null)
                        {

                            docu.Root.Element("Margins").Add(new XElement("GroupBackButton", new XAttribute("Top", this.TopMarTxt.Text.Trim()), new XAttribute("Buttom", this.bMarTxt.Text.Trim()), new XAttribute("Left", this.leftMarTxt.Text.Trim()), new XAttribute("Right", this.rMarTxt.Text.Trim()), new XAttribute("Width", this.widthTxt.Text.Trim()), new XAttribute("Height",this.heightTxt.Text.Trim())));

                        }
                        else
                        {
                            docu.Root.Element("Margins").Element("GroupBackButton").SetAttributeValue("Top", this.TopMarTxt.Text.Trim());
                            docu.Root.Element("Margins").Element("GroupBackButton").SetAttributeValue("Buttom", this.bMarTxt.Text.Trim());
                            docu.Root.Element("Margins").Element("GroupBackButton").SetAttributeValue("Left", this.leftMarTxt.Text.Trim());
                            docu.Root.Element("Margins").Element("GroupBackButton").SetAttributeValue("Right", this.rMarTxt.Text.Trim());
                            docu.Root.Element("Margins").Element("GroupBackButton").SetAttributeValue("Width", this.widthTxt.Text.Trim());
                            docu.Root.Element("Margins").Element("GroupBackButton").SetAttributeValue("Height", this.heightTxt.Text.Trim());

                        }






                    }


                    docu.Save(settingFpath);


                }



                MessageBox.Show("Settings saved for GOBackLabel !!");
            
            
            }










        }

        private void label1_Click(object sender, EventArgs e)
        {

        } // End of the save button










    }
}
